﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using WebVideo.Areas.Admin.Models;
using CarlosAg.ExcelXmlWriter;
using WebVideo.Controllers;

namespace WebVideo.Areas.Admin.Controllers
{
    public class ReportViewController : Controller
    {
        //
        // GET: /Admin/ReportView/

        public ActionResult Index()
        {
            return View(ReportViewModel.Index());
        }
        #region "Delete"
        public string Delete(int Id)
        {
            try
            {
                ReportViewModel.Delete(Id);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }
        #endregion //Delete

        #region "doitrangthai"
        public ActionResult Ban(int Id, int l)
        {
            try
            {
                ReportViewModel.Ban(Id, l);
                return RedirectToAction("Index", "ReportView");
            }
            catch
            {
                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
        }
        #endregion

        #region "Xuat bao cao"
        public JsonResult saveExcel()
        {
            Workbook book = new Workbook();
            ReportController rp = new ReportController();
            IList<Report> TieuDe = new List<Report>();
            IList<Report> NoiDung = new List<Report>();
            string Name = "Danh sách báo cáo gia hạn lượt view";
            dbVideoDataContext db = new dbVideoDataContext();
            // tạo danh sách tiêu đề
            var query = db.EntityReport_Views.OrderByDescending(g => g.Date).ToList();

            Report k = new Report();
            k.Name =  "Tên video";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Doanh nghiệp";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Tình trạng";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Số lượt view gia hạn";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Ngày đổi";
            TieuDe.Add(k);

            foreach (var item in query)
            {
                k = new Report();
                k.Name = item.EntityVideo.Name;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.UserName;
                NoiDung.Add(k);

                k = new Report();
                if (item.Status == 1)
                    k.Name = "Đã đổi";
                else
                    k.Name = "Chưa đổi";
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.ViewExtend.ToString();
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.Date.ToString();
                NoiDung.Add(k);
            }

            book = rp.Index(book, TieuDe, NoiDung, Name);
            string name = "BaoCao_GiaHanLuotView_YeuQuangCao" + ".xls";
            var path = Path.Combine(Server.MapPath("~/Content/"), name);
            book.Save(path);
            return Json(name);
        }

        #endregion

    }
}
